package com.dzf.controller;

import com.dzf.entity.*;
import com.dzf.service.LargeFileService;
import com.dzf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户的公共模块
 */
@RestController
@RequestMapping("/api/public")
public class PublicController {

    @Autowired
    UserService userService;

    /**
     * 获取用户权限
     * @param authentication
     * @return
     */
    @GetMapping("/info/all")
    public Authentication getauthentication(Authentication authentication)
    {
        return authentication;
    }

    /**
     * 获得用户信息
     * @return
     */
    @GetMapping("/info/getUser")
    public Object getUser(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyUser myUser = userService.findByAccount((String) principal);
        System.err.println("principal="+principal);
        if (myUser==null)return null;
        if (myUser instanceof UserDetails) {

            return (UserDetails)myUser;
        }else
        {
            System.err.println("1");
            return String.valueOf(principal);
        }
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}
